<?php

/***  DOCUMENTATION LAYER

CeoBase Class

Name: CeoBase
Last Update: May 2007
Author: Tom at klenwell@gmail.com

DESCRIPTION
  Base class for CeoFramework and CeoKernel classes

METHODS
  MAGIC
  CeoBase($debug=0, $oid=null)      *php 4 constructor*
  __construct($debug, $oid)          *php 5 constructor*
  __destruct()  
  
  PUBLIC
  e($message, $level=E_USER_NOTICE)
  tx()
  tx_diff($mt_last=0, $mt_now=0)
  print_d($message, $color='#c33')
  print_r($Mixed)
  dump()
  dump_debug_log($dirpath='')
  
  PRIVATE
  _load_fso()
  _load_err()
  _purge_passwords($string)
  _set_session_data()
  _get_session_data()
  _has_session_data()
  _set_filename()
  
USAGE
  This class serves as an interface and should not be called directly.

NOTES
  Encapsulates the $Err and $Fso (File System Object) classes.

______________________________________________________________________________*/

// Load File of Base Class (n/a)


// CeoBase
/*____________________________________________________________________________*/
class CeoBase
{
/* PUBLIC PROPERTIES */
var $debug = 0;
var $class_name = __CLASS__;
var $oid = '';
var $DS = DIRECTORY_SEPARATOR;

// Encapsulated Objects
var $Fso = 0;
var $Err = 0;

// Public Arrays
var $DIR = array();
var $PATH = array();
var $URL = array();

var $ceo_root = '';
var $doc_root = '';
var $super_root = '';
var $project_root = '';
var $script_path = '';

// mail config
var $word_wrap = 75;
var $use_smtp = 0;

/* PRIVATE PROPERTIES */
var $_filename = '';
var $_dirpath = '';

// Private Arrays
var $_ERR = array();      // error
var $_DBG = array();      // debug

/* ** MAGIC METHODS ** */
// php4 constructor
function CeoBase($debug=0, $oid=null)
{
  $this->__construct($debug, $oid);
  register_shutdown_function( array($this, '__destruct') );
}
// END constructor

// php5 constructor
function __construct($debug=0, $oid=null)
{
  // default
  $this->debug = $debug;
  $this->oid = ( empty($oid) ) ? $this->class_name : $oid;
  $this->_set_filename();
  $this->_set_dirpath();
  
  // debug
  if ( $this->debug ) $this->print_d('debugging is active for oid ' . $this->oid);
  if ( $this->debug ) $this->print_d('constructor complete for class ' . __CLASS__);
}
// END constructor

// destructor
function __destruct()
{
  if ( $this->debug ) $this->print_d("destroying class {$this->class_name} (oid: {$this->oid})");
}
// END destructor



/* ** PUBLIC METHODS ** */
// method: e (trigger error)
function e($message, $level=E_USER_NOTICE)
{
  $this->Err->trigger($message, $level);
  return;
}
// END method


// method: stage timer
function tx()
{
  $lapse = 0;   // return
  static $mt_now;
  
  if ( empty($mt_now) )
  {
    if ( $this->debug ) $this->print_d('first call, return 0'); 
    $mt_now = microtime();
    return $lapse;
  }
  else
  {
    $mt_last = $mt_now;
    $mt_now = microtime();
  }
  
  $lapse = $this->tx_diff($mt_last, $mt_now); 
  if ( $this->debug ) $this->print_d("mt_now - mt_last -> lapse : $mt_now - $mt_last -> $lapse");
   
  return $lapse;
}
// END method


// method: stage timer
function tx_diff($mt_last=0, $mt_now=0)
{
  if ( empty($mt_last) )
  {
    $this->e('must provide microtime value', E_USER_NOTICE);
    return 0;
  }
  
  $mt_now = ( empty($mt_now) ) ? microtime() : $mt_now;
  $lapse = ( substr($mt_now,0,9) + substr($mt_now,-10) ) - ( substr($mt_last,0,9) + substr($mt_last,-10) );
  $lapse = number_format($lapse,4);
 
  return $lapse;
}
// END method


// method: print_d
function print_d($message, $color='#c33')
{
  $_D = debug_backtrace();
  $f = basename($_D[0]['file']);
  $l = $_D[0]['line'];
  $loc = "{$f}:{$l}";
  $out = "<div style='line-height:1.5em; font-family:monospace; color:$color;'>$message <span style='color:#666;'>[$loc]</span></div>";
  $this->_DBG[] = "$loc -> " . strip_tags($message);
  echo $out;
  return;
}
// END method

// method: print_r
function print_r($Mixed)
{
  $return = htmlspecialchars(print_r($Mixed, 1));
  $return = "<pre>$return</pre>";
  return $return;
}
// END method

// method: dump
function dump()
{
  echo $this->print_r($this);
  return;
}
// END method

// method: dump debug log
function dump_debug_log($dirpath='')
{
  $_output = '';
  $_d1 = date('YmdHi');
  $_r = date('r');
  
  if ( empty($dirpath) ) $dirpath = $this->DIR['log_bin'];
  $fname = $this->class_name . ".debug.{$_d1}.log";
  $fpath = $dirpath . $fname;
  
  if ( $this->debug ) $this->print_d("debug dump to file [$fname] (don't forget to clean out dir [$dirpath])");
  
  $i = 0;
  foreach ( $this->_DBG as $_msg ) 
  {
    $i++;
    $si = sprintf('%03s',$i);
    $_msg = strip_tags($_msg);
    $_output .= "[$si] $_msg\n";
  }
  
  // header
  $_header = <<<TXT
debug log
$_r


TXT;

  $this->Fso->overwrite($fpath, $_header.$_output);
  return;
}
// END method



/* ** PRIVATE METHODS ** */
// method: load file system object
function _load_fso()
{
  // already loaded
  if ( is_object($this->Fso) ) return;
  
  require_once($this->PATH['fso_driver']);
  $this->Fso = new CeoFso($this->debug);
  return;
}
// END method

// method: load err object
function _load_err()
{
  // already loaded
  if ( is_object($this->Err) ) return;
  
  require_once($this->PATH['err_driver']);
  $this->Err = new CeoErr($this->debug);
  return;
}
// END method


// method: purge passwords from text (a quick and dirty regex match)
function _purge_passwords($string)
{
  $purged_string = '';    // return
  if ( $this->debug ) $this->print_d('purging passwords from string');
  
  // some parameters
  $sub = " ***pw purged***\n";
  $regex = '%(passw[^\n]*\n)|(pw[^\n]*\n)%';

  // to simply and make case insensitive
  $purged_string = strtolower($string);

  // we look for any span of text between 'passw' and an end line
  $purged_string = preg_replace($regex, $sub, $purged_string);

  return $purged_string;
}
// END method


// method: _set_session_data
function _set_session_data()
{
  // initialize session
  if ( !session_id() ) session_start(); 
  $_SESSION[$this->oid] = array();
  
  // add session data here
  
  return;
}
// END method

// method: get session data
function _get_session_data()
{
  // initialize session
  if ( !$this->_has_session_data() ) return; 
    
  // retrieve session variables
  // $this->var = $_SESSION[$this->oid]['var'];
  
  return;
}
// END method

// method: has session data
function _has_session_data()
{
  // initialize session
  if ( !session_id() ) session_start(); 
    
  // retrieve session variables
  if ( empty($_SESSION[$this->oid]) ) return 0;
  else return 1;
}
// END method

function _set_filename() { $this->_filename = basename(__FILE__); }
function _set_dirpath() { $this->_dirpath = dirname(__FILE__) . $this->DS; }

} // end class
/*____________________________________________________________________________*/

?>
